package com.spark.incoicings.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.hutool.poi.excel.ExcelExtractorUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.spark.incoicings.annotation.MyLog;
import com.spark.incoicings.dao.OrderMapper;
import com.spark.incoicings.service.*;
import com.spark.incoicings.vo.ProductVO;
import com.spark.incoicings.vo.PurchaseOrderVO;

import com.spark.invoicings.entity.Emp;
import com.spark.invoicings.spark_enum.SparkEnum;
import com.spark.invoicings.utils.EncryptUtil;
import com.spark.invoicings.utils.Result;
import com.spark.incoicings.dao.EmpMapper;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

@RestController
@RequestMapping("/login")
public class EmpController {
    @Resource
    private EmpMapper empMapper;
    @Resource
    private OrderService orderService;
    @Resource
    private SalesOrderService salesOrderService;
    @Resource
    private SalesReceiptService salesReceiptService;
    @Resource
    private SalesReturnService salesReturnService;
    @Resource
    private PaymentService paymentService;
    @Resource
    private ProductService productService;

    @PostMapping("/login")
    public Result login(String account,String pwd){
        QueryWrapper<Emp> wrapper = new QueryWrapper<>();
        wrapper.eq("account",account);
        Emp emp = empMapper.selectOne(wrapper);
        if (emp==null){
            return Result.error(SparkEnum.LOGIN_ERROR.getCode(), SparkEnum.LOGIN_ERROR.getMsg(), null);
        }else {
            if (!EncryptUtil.Encrypt(pwd).equals(EncryptUtil.Encrypt(emp.getPassword()))){
                return Result.error(SparkEnum.LOGIN_EXCEPTION.getCode(),SparkEnum.LOGIN_EXCEPTION.getMsg(), null);
            }else{
                return Result.success(SparkEnum.LOGIN_SUCCESS.getCode(),SparkEnum.LOGIN_SUCCESS.getMsg(), emp);
            }
        }
    }

    /**
     * 采购报表统计
     * @return
     */
    @GetMapping("/selectCount")
    public Result selectCount(){
        List<PurchaseOrderVO> purchaseOrderVOS = orderService.selectCount();
        return Result.success(200,"success",purchaseOrderVOS);
    }
    /**
     * 订单利润报表
     */
    @GetMapping("/selectSalesOrderCount")
    public Result selectSalesOrderCount(){
        return salesOrderService.selectSalesCount();
    }
    /**
     * 统计待收款金额
     */
    @GetMapping("/selectSalesOrderAmount")
    public Result selectSalesOrderAmount(){
        return salesOrderService.selectSalesOrderAmount();
    }
    /**
     * 统计应付总额
     */
    @GetMapping("/selectPendingAmount")
    public Result selectPendingAmount(){
        return orderService.selectPendingAmount();
    }
    /**
     * 统计近两年销售月回款总金额
     */
    @GetMapping("/selectReceiptCount")
    public Result selectReceiptCount(){
        return salesReceiptService.selectReceiptCount();
    }
    /**
     * 统计近两年销售退款支出
     */
    @GetMapping("/selectSalesReturnCount")
    public Result selectSalesReturnCount(){
        return salesReturnService.selectSalesReturnCount();
    }
    /**
     * 统计近两年月采购支出
     */
    @GetMapping("/selectPaymentCount")
    public Result selectPaymentCount(){
        return paymentService.selectPaymentCount();
    }
    /**
     * 统计产品销售排行
     */
    @GetMapping("/selectProductCount")
    public Result selectProductCount(){
        List<ProductVO> productVOS = productService.selectProductCount();
        return Result.success(200,"success",productVOS);
    }

    /**
     * 导出产品销售排行
     * @return
     */
    @GetMapping("/export")
    public void selectProductCount(HttpServletResponse response)throws Exception{
//        List<ProductVO> productVOS = productService.selectProductCount();
//        Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户","用户信息"),
//                ProductVO.class, productVOS);
//        FileOutputStream fos = new FileOutputStream("/Users/hello/Documents/easypoi-user1.xls");
//        workbook.write(fos);
//        fos.close();

        // 指定文件路径
        String filePath = "E:/cs/product.xls";

        try {
            // 准备数据，这里假设 productService.selectProductCount() 返回 ProductVO 列表
            List<ProductVO> productVOS = productService.selectProductCount();
            // 使用 EasyPoi 导出 Excel
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("产品销售排行", "产品信息"),
                    ProductVO.class, productVOS);
            // 创建文件对象
            File file = new File(filePath);
            // 如果文件不存在，则创建新文件
            if (!file.exists()) {
                file.createNewFile();
                System.out.println("文件已创建：" + filePath);
            }
            // 创建 FileOutputStream 对象
            FileOutputStream fos = new FileOutputStream(file);
            // 将 Workbook 写入到 FileOutputStream 中
            workbook.write(fos);
            // 关闭 FileOutputStream
            fos.close();
            // 关闭 Workbook
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 统计客户订单总额
     */
    @GetMapping("/selectCustomerOrderCount")
    public Result selectCustomerOrderCount(){
        return salesOrderService.selectCustomerOrderCount();
    }
    /**
     * 统计销售需求分析
     * @return
     */
    @GetMapping("/selectSaleCount")
    @MyLog(title = "主页模块",content = "统计销售需求分析")
    public Result selectSaleCount(){
        return salesOrderService.selectSaleCount();
    }
    /**
     * 采购在途情况
     */
    @GetMapping("/selectSourcing")
    @MyLog(title = "主页模块",content = "统计采购在途情况")
    public Result selectSourcing(){
        return orderService.selectSourcing();
    }


}
